import { BeforeInsert, Column, CreateDateColumn, DeleteDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm'
import { IsEmail } from 'class-validator'
import { genSaltSync, hashSync } from 'bcryptjs'

@Entity('user')
export class UserEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column()
  username: string

  @Column()
  @IsEmail()
  email: string

  @Column({ default: '' })
  bio: string

  @Column()
  password: string

  @Column()
  image: string

  @CreateDateColumn()
  created_at: Date

  @UpdateDateColumn()
  updated_at: Date

  @DeleteDateColumn()
  deleted_at: Date

  @BeforeInsert()
  async hashPassword() {
    const salt = genSaltSync(10)
    this.password = hashSync(this.password, salt)
  }
}

